import {
  getPlanListApi,
  abandonPlanApi,
  finishPlanApi,
} from '../../../api/app.js';
const app = getApp ();
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['全部', '工作', '学习', '娱乐', '生活', '其他'], // tabs 侧边栏
    tabActive: 0,
    currentPlanData: null,
    planList: [], // 自制计划列表
    isShowRemindModal: false, // 是否显示提醒弹窗
    currentDesc: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCurrentPlan (0);
  },
  openDesc (e) {
    console.log (e);
    const {index} = e.currentTarget.dataset;
    this.setData ({
      currentDesc: index,
    });
    this.showModal ();
  },
  showModal (e) {
    this.setData ({
      isShowRemindModal: !this.data.isShowRemindModal,
    });
  },
  // 选择tab
  selectTab (e) {
    const index = e.currentTarget.dataset.index;
    if (index == this.data.tabActive) return;
    this.setData ({
      tabActive: index,
    });
    this.getCurrentPlan (index);
  },
  // 获取计划
  async getCurrentPlan (type) {
    try {
      // 何时传true何时传false
      const res = await getPlanListApi ({
        uid: wx.getStorageSync ('openId'),
        planType: type || 0,
      });
      if (res.code == 200) {
        this.setData ({
          planList: res.result,
        });
      }
      console.log (res);
    } catch (error) {
      console.log (error);
    }
  },

  // 创建计划
  toCreact () {
    wx.navigateTo ({
      url: `/subPages/clockin/creact-plan/index?planType=${this.data.tabActive}`,
    });
  },
  // 切换Tab
  changeTab (e) {
    let tab = e.currentTarget.dataset.index;
    this.setData ({
      currentTab: tab,
    });
  },
  // 放弃计划
  abandon (e) {
    wx.showModal ({
      title: '温馨提示',
      content: '是否要放弃该计划？',
      success: res => {
        if (res.confirm) {
          const id = e.currentTarget.dataset.index;
          this.abandonPlan (id);
        }
      },
    });
  },
  finish (e) {
    // wx.showModal ({
    //   title: '温馨提示',
    //   content: '你已完成该计划？',
    //   success: res => {
    //     if (res.confirm) {

    //     }
    //   },
    // });
    const id = e.currentTarget.dataset.index;
    const status = e.currentTarget.dataset.status;
    this.finishPlan (id, status);
  },
  // 完成
  async finishPlan (id, status) {
    console.log (id,status);
    try {
      const res = await finishPlanApi ({
        id,
        status: status == '未完成' ? '完成' : '未完成',
      });
      if (res.code == 200) {
        app.toast ('操作成功！');
        this.getCurrentPlan (this.data.tabActive);
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 放弃
  async abandonPlan (id) {
    console.log (id);
    try {
      const res = await abandonPlanApi ({
        id,
      });
      if (res.code == 200) {
        app.toast ('放弃成功！');
        this.getCurrentPlan (this.data.tabActive);
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 新增计划
  toCreactPlan () {
    wx.navigateTo ({
      url: `/subPages/clockin/creact-plan/index`,
    });
  },
  // 新增计划
  toEdit (e) {
    const {index} = e.currentTarget.dataset;
    wx.navigateTo ({
      url: `/subPages/clockin/creact-plan/index?data=${JSON.stringify (index)}`,
    });
  },
});
